﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace IsolatedStorageList
{
    public partial class MainPage : PhoneApplicationPage
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void HyperlinkButton1_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton clinkLink = (HyperlinkButton)sender;
            string uri = string.Format("/IsolatedStorageList;component/SecondPage.xaml?id={0}",clinkLink.Content);
            NavigationService.Navigate(new Uri(uri, UriKind.Relative));
        }

        private void FIll_Click(object sender, RoutedEventArgs e)
        {
            CreateFile("test1.txt", "this is first txt.");
            CreateFile("test2.txt", "this is second txt.");
            CreateFile("test3.txt", "this is third txt.");
            BindList();
        }

        private void CreateFile(string fileName, string fileContent)
        {
            var storge = IsolatedStorageFile.GetUserStoreForApplication();
            if(!storge.FileExists(fileName))
            {
                using (var file=storge.CreateFile(fileName))
                {
                    using (var writter=new System.IO.StreamWriter(file))
                    {
                        writter.WriteLine(fileContent);
                    }
                }
            }
        }

        private void BindList()
        {
            var appStorga = IsolatedStorageFile.GetUserStoreForApplication();
            string[] fileName = appStorga.GetFileNames();
            Listbox1.ItemsSource = fileName;
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            BindList();
        }
    }
}
